<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0,user-scalable=no,minimal-ui">
    <title>菩提阁</title>
    <link rel="icon" href="./../images/favico.ico">
    <!--不同屏幕尺寸根字体设置-->
    <script src="./../js/base.js"></script>
    <!--element-ui的样式-->
    <link rel="stylesheet" href="../../backend/plugins/element-ui/index.css" />
    <!--引入vant样式-->
    <link rel="stylesheet" href="../styles/vant.min.css"/>
    <!-- 引入样式  -->
    <link rel="stylesheet" href="../styles/index.css" />
    <link rel="stylesheet" href="../styles/page.css" />
    <link rel="stylesheet" href="../styles/common.css" />
    <!--本页面内容的样式-->
    <link rel="stylesheet" href="./../styles/address-edit.css" />
</head>
<body>
<div id="address_edit" class="app">
    <div class="divHead">
        <div class="divTitle">
            <i class="el-icon-arrow-left" @click="goBack"></i>{{title}}
        </div>
    </div>
    <div class="divContent">
        <div class="divItem">
            <span>姓名：</span>
            <el-input placeholder="请填姓名" v-model="form.name"  maxlength='4' class="inputUser"/></el-input>
        </div>
        <div class="divItem">
            <span>性别：</span>
            <span class="spanChecked" @click="form.sex = '1'">
                    <i :class="{iActive:form.sex === '1'}"></i>
                    先生
            </span>
            <span class="spanChecked" @click="form.sex = '0'">
                    <i :class="{iActive:form.sex === '0'}"></i>
                    女士
            </span>
        </div>
        <div class="divItem">
            <span>手机号码：</span>
            <el-input placeholder="请输入手机号码" v-model="form.phone"  maxlength='17'/></el-input>
        </div>
        <div class="divItem">
            <span>身份证号：</span>
            <el-input placeholder=" 请输入身份证号" v-model="form.idNumber"  maxlength='18'/></el-input>
        </div>
        <div class="divSave" @click="updateUser">修改</div>
    </div>
</div>
<!-- 开发环境版本,包含了有帮助的命令行警告 -->
<script src="../../backend/plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../backend/plugins/element-ui/index.js"></script>
<!-- 引入vant样式 -->
<script src="./../js/vant.min.js"></script>
<script src="./../js/common.js"></script>
<script src="./../api/user.js"></script>
<!-- 引入axios -->
<script src="../../backend/plugins/axios/axios.min.js"></script>
<script src="./../js/request.js"></script>
<script>
    new Vue({
        el:"#address_edit",
        data(){
            return {
                title:'修改个人信息',
                form:{
                    id:'',
                    name:'',//姓名
                    phone:'',//手机号
                    sex:'1',//0表示女 1 表示男
                    avatar:'',//图片地址
                    idNumber:'',//身份证号码
                },
                id:undefined,
                activeIndex :0
            }
        },
        computed:{},
        created(){
            this.form.phone =sessionStorage.getItem("userPhone")
            this.getUser()
            this.initData()
        },
        mounted(){
        },
        methods:{
            goBack(){
                history.go(-1)
            },
            async getUser(){
                //参数封装
                const params = {
                    phone: this.form.phone
                }
                const res = await getUser(params)
                if (res.code === 1){
                    this.form = res.data
                }else {
                    this.$notify({ type:'warning', message:res.msg});
                }
            },
            async initData(){
                const userRes = await getUser(this.phone)
                if (userRes.code === 1){
                     this.form = userRes.data
                }
            },
            handleAvatarSuccess (response, file, fileList) {
                this.imageUrl = `/common/download?name=${response.data}`
            },
            beforeUpload (file) {
                if(file){
                    const suffix = file.name.split('.')[1]
                    const size = file.size / 1024 / 1024 < 2
                    if(['png','jpeg','jpg'].indexOf(suffix) < 0){
                        this.$message.error('上传图片只支持 png、jpeg、jpg 格式！')
                        this.$refs.upload.clearFiles()
                        return false
                    }
                    if(!size){
                        this.$message.error('上传文件大小不能超过 2MB!')
                        return false
                    }
                    return file
                }
            },
            onChange (file) {
                if(file){
                    const suffix = file.name.split('.')[1]
                    const size = file.size / 1024 / 1024 < 2
                    if(['png','jpeg','jpg'].indexOf(suffix) < 0){
                        this.$message.error('上传图片只支持 png、jpeg、jpg 格式！')
                        this.$refs.upload.clearFiles()
                        return false
                    }
                    if(!size){
                        this.$message.error('上传文件大小不能超过 2MB!')
                        return false
                    }
                    return file
                }
            },
            updateUser(){
                //进行参数判断
                const form = this.form
                if(!form.name){
                    this.$notify({ type:'warning', message:'请输入姓名'});
                    return
                }
                if(!form.idNumber){
                    this.$notify({ type:'warning', message:'请输入身份证号码'});
                    return
                }
                // const regex = /^(13[0-9]{9})|(15[0-9]{9})|(17[0-9]{9})|(18[0-9]{9})|(19[0-9]{9})$/;
                const regex = /^[1-9][0-9]{4,10}@qq.com$/;
                if(!regex.test(form.phone)){
                    this.$notify({ type:'warning', message:'手机号码不合法'});
                    return
                }
                //进行调用方法,更新用户信息
                let res = updateUser({id: this.form.id, name: this.form.name, phone: this.form.phone, avatar: '', sex: this.form.sex, idNumber: this.form.idNumber})
                sessionStorage.setItem("userPhone",this.form.phone)
                //页面跳转
                if(res.code === 1){
                    window.requestAnimationFrame(()=>{
                        window.location.href = '/front/page/user.html'
                    })
                }else{
                    this.$notify({ type:'warning', message:res.msg});
                }
            },
        }
    })
</script>
</body>
</html>
